<template>
  <div class="app-container">
<!--    班组form模糊查询-->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="班组类型" prop="name">
         <el-select v-model="queryParams.typeId" placeholder="请选择班组类型" clearable>
            <el-option
              v-for="dict in dict.type.sys_class_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"></el-option>
         </el-select>
      </el-form-item>
      <el-form-item label="班组编码" prop="head">
        <el-input
          v-model="queryParams.teamCode"
          placeholder="请输入班组编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="班组名称" prop="tel">
        <el-input
          v-model="queryParams.teamName"
          placeholder="请输入班组名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
<!--  班组修改删除添加按钮-->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:market:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:market:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:market:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:market:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
<!--班组列表-->
    <el-table v-loading="loading" :data="marketList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="班组id" align="center" prop="teamId" />
      <el-table-column label="班组编码" align="center" prop="teamCode" />
      <el-table-column label="班组名称" align="center" prop="teamName" />
      <el-table-column label="所属分类" align="typeId" prop="typeId">
        <template v-slot="scope">
          <dict-tag :options="dict.type.sys_class_type" :value="scope.row.typeId"/>
        </template>
      </el-table-column>
<!--      <el-table-column label="班组类型" align="center" prop="dname" />-->
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:market:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:market:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
<!--    班组分页-->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加零售对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
        <!-- 你的对话框内容 -->
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="班组编码" prop="teamCode">
            <el-input v-model="uuidCode" placeholder="请输入班组编码" />
            <el-switch
              v-model="flaging"
              active-text="自动生成">
            </el-switch>
          </el-form-item>
          <el-form-item label="班组名称" prop="head">
            <el-input v-model="form.teamName" placeholder="请输入班组名称" />
          </el-form-item>
          <el-form-item label="班组类型" prop="head">
            <el-select v-model="form.typeId" placeholder="请选择班组类型" clearable>
              <el-option
                v-for="dict in dict.type.sys_class_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="备注" prop="tel">
            <el-input v-model="form.remark" placeholder="请输入备注" />
          </el-form-item>
        </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="addForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 修改零售对话框 -->
    <el-dialog :title="title" :visible.sync="openxiu" width="1000px" append-to-body>
      <!-- 你的对话框内容 -->
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="班组编码" prop="teamCode">
          <el-input v-model="uuidCode" placeholder="请输入班组编码" />
          <el-switch
            v-model="flaging"
            active-text="自动生成">
          </el-switch>
        </el-form-item>
        <el-form-item label="班组名称" prop="head">
          <el-input v-model="form.teamName" placeholder="请输入班组名称" />
        </el-form-item>
        <el-form-item label="班组类型" prop="head">
          <el-select v-model="form.typeId" placeholder="请选择班组类型" clearable>
            <el-option
              v-for="dict in dict.type.sys_class_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="tel">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>

      <span style="color: #8c939d">—————————————————————————————项目成员——————————————————————————————</span>
      <br>
      <el-button
        type="primary"
        plain
        icon="el-icon-plus"
        size="mini"
        @click="openaddUser"
        v-hasPermi="['crop:team:add']"
      >新增</el-button>
      <el-table v-loading="loading" :data="userList">
        <el-table-column type="selection" width="50" align="center" />
        <el-table-column label="用户名称" align="center" key="userName" prop="userName"/>
        <el-table-column label="用户昵称" align="center" key="nickName" prop="nickName" />
        <el-table-column label="手机号码" align="center" key="phonenumber" prop="phonenumber" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width"/>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="forms.pageNum"
        :limit.sync="forms.pageSize"
        @pagination="getList"
      />

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="updateForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加用户对话框 -->
    <el-dialog :title="title" :visible.sync="openadder" width="1000px" append-to-body>
      <el-row :gutter="20">
        <!--部门数据-->
        <el-col :span="4" :xs="24">
          <div class="head-container">
            <el-input
              v-model="deptName"
              placeholder="请输入部门名称"
              clearable
              size="small"
              prefix-icon="el-icon-search"
              style="margin-bottom: 20px"
            />
          </div>
          <div class="head-container">
            <el-tree
              :data="deptOptions"
              :props="defaultProps"
              :expand-on-click-node="false"
              :filter-node-method="filterNode"
              ref="tree"
              node-key="id"
              default-expand-all
              highlight-current
              @node-click="handleNodeClick"
            />
          </div>
        </el-col>
        <!--用户数据-->
        <el-col :span="20" :xs="24">
          <el-form :model="query" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item label="用户名称" prop="userName">
              <el-input
                v-model="query.userName"
                placeholder="请输入用户名称"
                clearable
                style="width: 240px"
                @keyup.enter.native="handleQuerys"
              />
            </el-form-item>
            <el-form-item label="手机号码" prop="phonenumber">
              <el-input
                v-model="query.phonenumber"
                placeholder="请输入手机号码"
                clearable
                style="width: 240px"
                @keyup.enter.native="handleQuerys"
              />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuerys">搜索</el-button>
              <el-button icon="el-icon-refresh" size="mini" @click="resetQuerys">重置</el-button>
            </el-form-item>
          </el-form>

          <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChanges">
            <el-table-column type="selection" width="50" align="center" />
            <el-table-column label="用户编号" align="center" key="userId" prop="userId"/>
            <el-table-column label="用户名称" align="center" key="userName" prop="userName"  />
            <el-table-column label="用户昵称" align="center" key="nickName" prop="nickName" />
            <el-table-column label="部门" align="center" key="deptName" prop="dept.deptName"/>
            <el-table-column label="手机号码" align="center" key="phonenumber" prop="phonenumber" />
            <el-table-column label="状态" align="center" key="status">
              <template slot-scope="scope">
                <el-switch
                  v-model="scope.row.status"
                  active-value="0"
                  inactive-value="1"
                  @change="handleStatusChange(scope.row)"
                ></el-switch>
              </template>
            </el-table-column>
            <el-table-column label="创建时间" align="center" prop="createTime">
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.createTime) }}</span>
              </template>
            </el-table-column>
          </el-table>

          <pagination
            v-show="total>0"
            :total="total"
            :page.sync="query.pageNum"
            :limit.sync="query.pageSize"
            @pagination="userZong"
          />
        </el-col>
      </el-row>


      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="addAround">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMarket, getMarket, delMarket, addMarket, updateMarket,TypeLists} from "@/api/scheduling/team";
import {selectMaterialCode} from "@/api/scheduling/plan";
import {changeUserStatus, deptTreeSelect, listUser} from "@/api/system/user";
import Treeselect from "@riophae/vue-treeselect";

export default {
  name: "Market",
  dicts: ['sys_class_type'],
  components: { Treeselect },
  watch:{
    flaging(val){
      if(val){
        this.getMaterialCode();
      }
    },
    // 根据名称筛选部门树
    deptName(val) {
      this.$refs.tree.filter(val);
    }
  },
  data() {
    return {
      // 日期范围
      dateRange: [],
      //新增用户fenye
      query:{
        pageNum:1,
        pageSize:10,
        userName: undefined,
        phonenumber: undefined,
        status: undefined,
        deptId: undefined
      },
      // 部门名称
      deptName: undefined,
      // 部门树选项
      deptOptions: undefined,
      defaultProps: {
        children: "children",
        label: "label"
      },
      //打开用户新增
      openadder:false,
      userList:[],
      openxiu:false,
      uuidCode:undefined,
      flaging:false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      //pageNum
     forms:{
       pageNum:1,
       pageSize:10
     },
      totals:0,
      // 总条数
      total: 0,
      // 零售表格数据
      marketList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        head: null,
        tel: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getDeptTree();
    this.userZong();
  },
  methods: {
    //添加用户通过id
    addAround(){

    },
    //用户重置
    /** 重置按钮操作 */
    resetQuerys() {
      this.resetForm("query");
      this.handleQuerys();
    },
    //用户搜索
    /** 搜索按钮操作 */
    handleQuerys() {
      this.query.pageNum = 1;
      this.userZong();
    },

    // 用户多选框选中数据
    handleSelectionChanges(selection) {
      this.ids = selection.map(item => item.userId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 查询用户列表 */
    userZong() {
      this.loading = true;
      listUser(this.addDateRange(this.query, this.dateRange)).then(response => {
          this.userList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    /** 查询部门下拉树结构 */
    getDeptTree() {
      deptTreeSelect().then(response => {
        this.deptOptions = response.data;
      });
    },
    // 用户状态修改
    handleStatusChange(row) {
      let text = row.status === "0" ? "启用" : "停用";
      this.$modal.confirm('确认要"' + text + '""' + row.userName + '"用户吗？').then(function() {
        return changeUserStatus(row.userId, row.status);
      }).then(() => {
        this.$modal.msgSuccess(text + "成功");
      }).catch(function() {
        row.status = row.status === "0" ? "1" : "0";
      });
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.query.deptId = data.id;
      console.log(data);
      console.log( this.query.deptId)
      this.handleQuerys();
    },
    //打开用户新增
    openaddUser(){
      this.reset();
      this.openadder = true;
      this.title = "添加用户";
    },
    //班组主表修改
    updateForm(){
      updateMarket(this.form).then(response => {
        this.$modal.msgSuccess("修改成功");
        this.open = false;
        this.getList();
      });
    },
    //班组主表添加
    addForm(){
      this.form.teamCode=this.uuidCode;
      addMarket(this.form).then(response => {
        this.$modal.msgSuccess("新增成功");
        this.open = false;
        this.getList();
      });
    },
    //自动生成编码
    getMaterialCode(){
      selectMaterialCode().then(r=>{
        this.uuidCode=r.msg;
      })
    },
    /** 查询班组列表 */
    getList() {
      this.loading = true;
      listMarket(this.queryParams).then(response => {
        this.marketList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.openxiu = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        head: null,
        tel: null
      };
      this.resetForm("form");
    },
    //班组
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    //班组
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    // 班组多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.teamId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },

    /** 班组新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加零售";
    },
    /** 班组修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.title = "修改零售";
      const teamId = row.teamId || this.ids;
      this.uuidCode=row.teamCode;
      console.log(teamId)
      getMarket(teamId).then(response => {
        this.form = response.data;
        this.form.typeId=row.typeId;
        this.openxiu = true;
      });
    },
    /** 班组删除按钮操作 */
    handleDelete(row) {
      const ids = row.teamId || this.ids;
      this.$modal.confirm('是否确认删除零售编号为"' + ids + '"的数据项？').then(function() {
        return delMarket(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/market/export', {
        ...this.queryParams
      }, `market_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
